﻿using System;
using System.Web.Services;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.Constants;
using Monkey.Warrenty.EntityModel;
using MonkeyWarranty.Web.Common;

namespace MonkeyWarranty.Web.Employees
{
    public partial class List : System.Web.UI.Page
    {
        #region Properties

        private static EmployeeBiz _employeeBiz;
        private static EmployeeBiz EmployeeManager
        {
            get { return _employeeBiz ?? (_employeeBiz = new EmployeeBiz()); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WarrantyCommon.CurrrentEmployee == null)
            {
                Response.Redirect("~/Logon.aspx");
                return;
            }

            if (!WarrantyCommon.CurrrentEmployee.HasRole(RoleConstant.Admin))
            {
                Response.Redirect("~/NoPermission.aspx");
            }
        }

        [WebMethod(EnableSession = true)]
        public static object EmployeeList()
        {
            return EmployeeManager.GetAllEmployeesWs();
        }

        [WebMethod(EnableSession = true)]
        public static object CreateEmployee(Employee record)
        {
            return EmployeeManager.CreateEmployeeWs(record);
        }

        [WebMethod(EnableSession = true)]
        public static object UpdateEmployee(Employee record)
        {
            return EmployeeManager.UpdateEmployeeWs(record);
        }

        [WebMethod(EnableSession = true)]
        public static object DeleteEmployee(int Id)
        {
            if (WarrantyCommon.CurrrentEmployee != null && WarrantyCommon.CurrrentEmployee.IsAdmin && WarrantyCommon.CurrrentEmployee.Id != Id)
            {
                return EmployeeManager.DeleteEmployeeWs(Id);
            }
            return new { Result = "ERROR", Message = "Error" };
        }
    }
}